package com.nescafe.nescafepromo.command.user;

import java.util.List;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Query;
import com.nescafe.nescafepromo.command.Command;
import com.nescafe.nescafepromo.command.CommandException;
import com.nescafe.nescafepromo.entity.EntityType;
import com.nescafe.nescafepromo.model.NPRequest;
import com.nescafe.nescafepromo.model.NPResponse;

public class ViewHistory implements Command {

	@Override
	public NPResponse execute(NPRequest req) throws CommandException {
		NPResponse res = null;
		try {
			
			String email = req.getParam("email");
			Key participantKey = EntityType.PARTICIPANT.getKey(email);
			Query q = new Query();
			q.setAncestor(participantKey);
			DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
			List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
			
			res = new NPResponse(results);
			
		} catch (Exception e) {
			throw new CommandException(e.getMessage());
		}
		return res;
	}

}
